﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminTestimonialController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TestimonialModel model = new TestimonialModel();

                foreach (var testimonial in db.EntityTestimonials.OrderBy(o => o.DateTime))
                {
                    model.listTestimonial.Add(new Testimonial()
                    {
                        Id = testimonial.Id,
                        Name = testimonial.Name,
                        Review = testimonial.Review,
                        DateTime = testimonial.DateTime
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();
                EntityTestimonial item = db.EntityTestimonials.Where(p => p.Id == id).Single();
                db.EntityTestimonials.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";
            }
            catch
            {
                return "Error";

            }

        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                Testimonial model = new Testimonial();

                return PartialView(model);
            }        
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(Testimonial model)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTestimonial testimonial = new EntityTestimonial();
                testimonial.Name = model.Name;
                testimonial.Review = model.Review;
                testimonial.DateTime = DateTime.Now;

                db.EntityTestimonials.InsertOnSubmit(testimonial);
                db.SubmitChanges();

                return RedirectToAction("Index", "AdminTestimonial");
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int idTestimonial)
        {
            return PartialView();
        }

        [HttpPost]
        public JsonResult GetDetail(int idTestimonial)
        {
            Testimonial model = new Testimonial();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTestimonial testimonial = db.EntityTestimonials.FirstOrDefault(o => o.Id == idTestimonial);

                if (testimonial != null)
                {
                    model.Id = testimonial.Id;
                    model.Name = testimonial.Name;
                    model.Review = testimonial.Review;                    
                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int Id, string Name, string Review)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityTestimonial testimonial = db.EntityTestimonials.Where(p => p.Id == Id).FirstOrDefault();
                testimonial.Name = Name;
                testimonial.Review = Review;
                
                db.SubmitChanges();
                return RedirectToAction("Index", "AdminTestimonial");
            }
        }

        #endregion //edit
    }
}
